﻿/**
* ...
* @author Default
* @version 0.1
*/

package bdz.common.display.core {
	
	import bdz.common.events.DisplayItemEvent;
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.utils.getQualifiedClassName;
	
	public class AbstractDisplayItem extends MovieClip{
		
		private var _clickable:Boolean = true;
		protected var _info:*;
		protected var _dataField:String = "";
		
		function AbstractDisplayItem(access:AbstractRestriction){
			if(access == null){
				throw new Error("NullPointerException on Abstract Class :: " + getQualifiedClassName(AbstractDisplayItem));
			}
			addEventListener(Event.ADDED_TO_STAGE, initializeComponent);
			addEventListener(Event.REMOVED_FROM_STAGE, cleanUp);
		}
		protected static function getAccess():AbstractRestriction {
			return new AbstractRestriction();
		}
		protected function toggleClickable():void{
			useHandCursor = buttonMode = _clickable;
			
			if(_clickable){
				addEventListener(MouseEvent.CLICK, clicked);
				addEventListener(MouseEvent.MOUSE_OVER, rolledOver);
				addEventListener(MouseEvent.MOUSE_OUT, rolledOut);
			}else{
				removeEventListener(MouseEvent.CLICK, clicked);
				removeEventListener(MouseEvent.MOUSE_OVER, rolledOver);
				removeEventListener(MouseEvent.MOUSE_OUT, rolledOut);
			}
		}
		//Component initializations
		protected function initializeComponent(evt:Event):void{
			toggleClickable();
		}
		//Component cleanup
		protected function cleanUp(evt:Event):void{
			removeEventListener(Event.ADDED_TO_STAGE, initializeComponent);
			removeEventListener(Event.REMOVED_FROM_STAGE, cleanUp);
		}
		protected function onUpdateInfo():void {
			
		}
		protected function rolledOver(evt:MouseEvent):void{
			dispatchEvent(new DisplayItemEvent(DisplayItemEvent.ROLLED_OVER, _info));
		}
		protected function rolledOut(evt:MouseEvent):void{
			dispatchEvent(new DisplayItemEvent(DisplayItemEvent.ROLLED_OUT, _info));
		}
		protected function clicked(evt:MouseEvent):void{
			dispatchEvent(new DisplayItemEvent(DisplayItemEvent.CLICKED, _info));
		}
		public function set clickable(b:Boolean):void{
			_clickable = b;
			toggleClickable();
		}
		public function get clickable():Boolean{
			return _clickable;
		}
		public function set info(i:*):void{
			_info = i;
			onUpdateInfo();
		}
		public function get info():*{
			return _info;
		}
		public function set dataField(df:String):void{
			_dataField = df;
		}
		public function get dataField():String{
			return _dataField;
		}
		
	}
	
}
class AbstractRestriction {}